from asyncio import CancelledError
from asyncio import run
from src.config.settings import Settings
from src.tools import ColorfulConsole
import sys
import os
import json

# sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
# from TikTokTool.src.application import TikTokDownloader

# sys.path.append(os.path.dirname(__file__))
from src.application import TikTokDownloader
from src.gui_edition import gui_main
from src.custom import PROJECT_ROOT
async def main():
    async with TikTokDownloader() as downloader:
        try:
            await downloader.run()
        except (
                KeyboardInterrupt,
                CancelledError,
        ):
            return

def get_run_mode():
    # 读取当前配置
    settings = Settings(PROJECT_ROOT, ColorfulConsole())
    config_data = settings.read()
    run_mode = config_data.get("run_mode",1)
    # 默认模式（可以是0或1）
    return run_mode

if __name__ == "__main__":
    run_mode = get_run_mode()
    
    if run_mode == 1:
        # GUI模式
        gui_main()
    else:
        # 命令行模式
        run(main())